import axios from "axios";
import EventBus from "@/util/event";
import { type ResUserInfo } from "@/store/slice/userInfo";
const instance = axios.create({});

instance.interceptors.response.use(
	function (response) {
		// 2xx 范围内的状态码都会触发该函数。
		// 对响应数据做点什么
		if (response.data.code === -1) {
			EventBus.emit("global_error_tips", response.data.msg);
		}
		if (response.status === 200) {
			//没有登陆
			if (response.data.code === 401) {
				EventBus.emit("global_not_login", response.data.msg);
			}
		} else if (response.status === 403) {
			EventBus.emit("global_error_auth", "没有权限，别瞎访问");
		}

		return response;
	},
	function (error) {
		// 超出 2xx 范围的状态码都会触发该函数。
		// 对响应错误做点什么

		return Promise.reject(error);
	}
);
export type AxiosRes<T = ResData> = {
	config: Object;
	data: T;
	headers: any;
	request: any;
	status: number;
	statusText: string;
};
export type ResData<T = any> = {
	code: number;
	data: T;
	msg: string;
};

export default instance;
